
package com.tools.cleanmaster.ui.view.trash;

import android.content.Context;
import android.graphics.drawable.Drawable;

import com.tools.cleanmaster.CleanApplication;
import com.tools.cleanmaster.R;
import com.tools.cleanmaster.model.TrashType;
import com.tools.cleanmaster.model.item.TrashItem;

import java.util.ArrayList;
import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Set;

/**
 * 卸载残留group
 */
@SuppressWarnings("static-access")
public class TrashResidualGroup extends TrashGroup {

    /**
     *卸载残留一个子项，根据pkgNames或者说appName合并成的一条，内含多个路径
     */
    public static class AppResTrashItem extends TrashViewItemSingle {
        private ArrayList<TrashItem> mItems = null;
        public ArrayList<TrashItem> getItems() {
            return mItems;
        }

        private Context mContext;
        private long mSize = 0l;
        AppResTrashItem(TrashGroup group, ArrayList<TrashItem> items) {
            super(items.get(0), group);
            mContext = CleanApplication.getInstance();
            mItems = items;
            for (TrashItem item : mItems) {
                mSize += item.size;
            }
        }

        @Override
        public String getTitle() {
            return mTrashItem.appName;
        }

        @Override
        public Drawable getIcon() {
            return mContext.getResources().getDrawable(R.drawable.icon_trash_clean_residual_item);
        }

        @Override
        public long getTotalSize() {
            return mSize;
        }
    }

    public TrashResidualGroup() {
        super(null);
    }

    @Override
    public Drawable getIcon() {
        return mContext.getResources().getDrawable(R.drawable.icon_trash_clean_residual);
    }

    @Override
    public String getTitle() {
        return mContext.getResources().getString(R.string.residual_uninstall_trash_item);
    }

    /* (non-Javadoc)
     * @see TrashGroup#fillTrashes(java.util.Map)
     */
    @Override
    public void fillTrashes(Map<TrashType, List<TrashItem>> trashes) {
        List<TrashItem> list = trashes.get(TrashType.UNINSTALLED_APP);
        if (list != null && !list.isEmpty()) {
            Map<String, ArrayList<TrashItem>> pkgCacheMap = new HashMap<String, ArrayList<TrashItem>>();
            for (TrashItem trashItem : list) {
                ArrayList<TrashItem> items = null;
                String pkgName = trashItem.pkgName;
                if (!pkgCacheMap.containsKey(pkgName)) {
                    items = new ArrayList<TrashItem>();
                    pkgCacheMap.put(pkgName, items);
                } else {
                    items = pkgCacheMap.get(pkgName);
                }
                items.add(trashItem);
            }
            Set<String> pkgs = pkgCacheMap.keySet();
            for (String pkg : pkgs) {
                AppResTrashItem appResTrashGroup = new AppResTrashItem(this, pkgCacheMap.get(pkg));
                addChildItem(appResTrashGroup);
            }
            // 设置check状态
            setCheckStateByChild();
            Collections.sort(mChildItems);
        }
    }

    /* (non-Javadoc)
     * @see TrashGroup#getSelectedTrahItems()
     */
    @Override
    public ArrayList<TrashItem> getSelectedTrahItems() {
        ArrayList<TrashItem> selectedItems = new ArrayList<TrashItem>();
        for (TrashViewItem viewItem : mChildItems) {
            if (viewItem.isChecked() && viewItem instanceof AppResTrashItem) {
                selectedItems.addAll(((AppResTrashItem) viewItem).getItems());
            }
        }
        return selectedItems;
    }
}
